function [camA RR TT] = GetCameraS03MatrixOGL( pos, lookat, strname, fignum ) 
dbstop if error 
% the matrix that transforms the 
% camera to be at pos, viewing the origin
% The T part of the matrix is just [pos]

% This .m function is designed to follow the OpenGL "lookat()"
% http://www.opengl.org/documentation/specs/man_pages/hardcopy/GL/html/glu/lookat.html

if nargin < 3, strname = 'Camera Origin'; end
if nargin < 4, fignum = 1; end

camA = zeros(4,4); % we get a 4x4 matrix operating on homog. coordinates

lookat = lookat(:);
T = pos(:);
TT = eye(4,4); TT(4,4) = 1; TT(1:3,4) = -T;

dir = -(lookat-T) / norm(T-lookat); % direction from point to origin


up = [0;1;0];
s = cross(dir,up); % s  Perp dir,up
s = s / norm(s);
u = cross(s,dir);  % u  Perp s,dir
u = u / norm(u);
R = [s';u';-dir'];
RR = [R zeros(3,1); zeros(1,3) 1];
camA = RR * TT;  % for transforming points to the view
camAI = camA^-1; % reverse map

% draw a plot for debug purposes
p0 = 1 * 0.3 * norm(pos);
x_ = [ [0;0;0;1] [p0;0;0;1] ];
y_ = [ [0;0;0;1] [0;p0;0;1] ];
z_ = [ [0;0;0;1] [0;0;p0;1] ];
figure(fignum);
if nargin == 4 , hold on; end
plot3(x_(1,:),x_(2,:),x_(3,:),'r');  hold on
plot3(y_(1,:),y_(2,:),y_(3,:),'g'); plot3(z_(1,:),z_(2,:),z_(3,:),'b');
x_c = camAI * x_; y_c = camAI * y_; z_c = camAI * z_;
text(0,0,0,'World Origin','FontSize',8);
figure(fignum); plot3(x_c(1,:),x_c(2,:),x_c(3,:),'r');  
plot3(y_c(1,:),y_c(2,:),y_c(3,:),'g'); plot3(z_c(1,:),z_c(2,:),z_c(3,:),'b');
text(x_c(1),x_c(2),x_c(3),strname,'FontSize',8);
camproj('perspective'); xlabel('x'); ylabel('y');
hold off

breakhere = 1;